package com.chinacoal.hr.levelboot.schedule;

import com.chinacoal.hr.levelimpl.service.AttendanceInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author: YunTao.Li
 * @create: 2020-05-18 12:05
 * @description: 定时器服务
 **/
@Slf4j
@Component
@PropertySource({"classpath:application.yml"})
public class ScheduledService {

    @Autowired
    private AttendanceInfoService attendanceInfoService;

    /**
     * 考勤信息同步定时器
     *
     * @return : void
     * @author : YunTao.Li
     * @date : 2020/5/18 2020/5/18
     */
    @Scheduled(cron = "${prop.attendance-sync-cron}")
    public void AttendanceInfoSyncTimer() {
        log.info("cron in properties");
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Date today_date = new Date();
        String today_date_str = format.format(today_date);

        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -2);
        String yesterday_date_str = format.format(c.getTime());

        try {
            this.attendanceInfoService.syncAttendanceInfo(yesterday_date_str, today_date_str);
        } catch (Throwable e) {
            e.printStackTrace();
            log.error("=================定时同步失败========================");
            log.error(e.getMessage());
        }
    }

//    @Scheduled(cron = "0/5 * * * * *")
//    public void scheduled() {
//        log.info("=====>>>>>使用cron  {}", System.currentTimeMillis());
//    }
//
//    @Scheduled(fixedRate = 5000)
//    public void scheduled1() {
//        log.info("=====>>>>>使用fixedRate{}", System.currentTimeMillis());
//    }
//
//    @Scheduled(fixedDelay = 5000)
//    public void scheduled2() {
//        log.info("=====>>>>>fixedDelay{}", System.currentTimeMillis());
//    }
}
